package com.awesumgames.testgame;

import com.awesumgames.testgame.R;
import com.awesumgames.testgame.Block.Type;
import com.awesumgames.testgame.Ball;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tv.ouya.console.api.OuyaController;

import static com.awesumgames.awesum.AwesumGlobal.Awesum;
import com.awesumgames.awesum.Game;
import com.awesumgames.awesum.Scene;
import com.awesumgames.awesum.Vector3;
import com.awesumgames.awesum.SceneCollection.SceneMode;

public class TestScene extends Scene {
	public Paddle paddle = null;
	public Ball ball = null;
	private String _levelName = "";
	
	public TestScene() {
		super();
	}

	public void load() {
		empty();
		
		JSONObject level = null;
		try {
			level = Awesum.Load.Json(R.raw.level1);
			if (level != null) {
				JSONArray jArr = null;
				JSONObject jObj = null;
				JSONArray pos = null;
				
				_levelName = level.getString("name");  
				
				//BLOCK
				jArr = level.getJSONArray("block");
				for (int i = 0; i < jArr.length(); ++i) {
					jObj = jArr.getJSONObject(i);
					
					pos = jObj.getJSONArray("pos");
					//Log.d("debug", jObj.getString("type"));
					String t = jObj.getString("type");
					Block.Type type;
					
					if (t.equals("b")) type = Block.Type.BOOST;
					else if (t.equals("sb")) type = Block.Type.SUPERBOOST;
					else if (t.equals("r")) type = Block.Type.REDUCE;
					else if (t.equals("br")) type = Block.Type.BREAK;
					else if (t.equals("s")) type = Block.Type.SLIDE;
					else type = Block.Type.NORMAL;
					children.add(new Block(type,
							new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))));
				}

				//PADDLE
				jArr = level.getJSONArray("paddle");
				for (int i = 0; i < jArr.length(); ++i) {
					jObj = jArr.getJSONObject(i);
					pos = jObj.getJSONArray("pos");
					String t = jObj.getString("type");
					Paddle.Type type;
					
					if (t.equals("r")) type = Paddle.Type.REVERSE;
					else if (t.equals("hl")) type = Paddle.Type.HALF_LEFT;
					else if (t.equals("hm")) type = Paddle.Type.HALF_MIDDLE;
					else if (t.equals("hr")) type = Paddle.Type.HALF_RIGHT;
					else if (t.equals("m")) type = Paddle.Type.MINI;
					else type = Paddle.Type.NORMAL;
					children.add(paddle = new Paddle(type,
							new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))));
				}
				
				//BALL
				jArr = level.getJSONArray("ball");
				for (int i = 0; i < jArr.length(); ++i) {
					jObj = jArr.getJSONObject(i);
					pos = jObj.getJSONArray("pos");
					children.add(ball = new Ball(
							new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		super.load();
		
		Game.camera.set(4.5f,	2f,	10f,
				4.5f,	2f,	-5f,
				0f,	1f,	0f);
	}

	public void draw() {
		super.draw();
	}

	public void step() {
		super.step();
		
		if (ball.pos.y > 4f)
			Game.camera.view.y = ball.pos.y - 2f;
		else
			Game.camera.view.y = 2f;
		
		float accX = Game.accelerometer.x;
			/*if (acc_x > 0) //.5f)
				paddle.moveLeft(acc_x);
			else if (acc_x < 0) //-.5f
				paddle.moveRight(Math.abs(acc_x));*/
		
		OuyaController c = OuyaController.getControllerByPlayer(0);
		if (c == null) return;
		if (c.buttonChangedThisFrame(OuyaController.BUTTON_R1) && c.buttonPressedThisFrame(OuyaController.BUTTON_R1)) {
			Game.scenes.add(new BreakoutScene(), SceneMode.REPLACE);
		}
	}
}
